package cn.hs.ph.servlet;

import cn.hs.ph.dao.UserDao;
import cn.hs.ph.dao.impl.UserDaoImpl;
import cn.hs.ph.entity.User;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        编码
//        处理请求信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        处理数据库信息
        UserDao userDao = new UserDaoImpl();
        User user = userDao.selectByUsernameAndPwd(username, password);
//        响应
        Result success = Result.error("登录失败");
        if (user != null) {
            success = Result.success("登录成功");
        }
        resp.getWriter().write(JSON.toJSONString(success));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
